using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace University.Models
{ 
    public class ProfessorRepository : IProfessorRepository
    {
        UniversityContext context = new UniversityContext();

        public IQueryable<Professor> All
        {
            get { return context.Professors; }
        }

        public IQueryable<Professor> AllIncluding(params Expression<Func<Professor, object>>[] includeProperties)
        {
            IQueryable<Professor> query = context.Professors;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Professor Find(int id)
        {
            return context.Professors.Find(id);
        }

        public void InsertOrUpdate(Professor professor)
        {
            if (professor.ProfessorId == default(int)) {
                // New entity
                context.Professors.Add(professor);
            } else {
                // Existing entity
                context.Entry(professor).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var professor = context.Professors.Find(id);
            context.Professors.Remove(professor);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }
    }

    public interface IProfessorRepository : IDisposable
    {
        IQueryable<Professor> All { get; }
        IQueryable<Professor> AllIncluding(params Expression<Func<Professor, object>>[] includeProperties);
        Professor Find(int id);
        void InsertOrUpdate(Professor professor);
        void Delete(int id);
        void Save();
    }
}